package gui;

import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;

import domein.PeerController;
import domein.Settings;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame
{
	private static final long serialVersionUID = 1L;
	private final static String APPLICATIE = "P2P";
	
	public MainFrame()
	{
		super(APPLICATIE);
		initGui();
	}
	
	public void initGui()
	{	
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		addWindowListener(new WindowListener()
		{
			@Override
			public void windowClosing(WindowEvent e) 
			{
				actionExit();
			}

			@Override
			public void windowActivated(WindowEvent arg0) {}

			@Override
			public void windowClosed(WindowEvent arg0) {}

			@Override
			public void windowDeactivated(WindowEvent arg0) {}

			@Override
			public void windowDeiconified(WindowEvent arg0) {}

			@Override
			public void windowIconified(WindowEvent arg0) {}

			@Override
			public void windowOpened(WindowEvent arg0) {}
		});
		
		JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem fileExitMenuItem = new JMenuItem("Exit");
        fileExitMenuItem.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                actionExit();
            }
        });
        
        fileMenu.add(fileExitMenuItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
        
        JMenu mnOptions = new JMenu("Options");
        menuBar.add(mnOptions);
        
        JMenuItem mntmEditSharedFolder = new JMenuItem("Edit shared folder");
        mntmEditSharedFolder.addActionListener(new ActionListener() 
        {
        	public void actionPerformed(ActionEvent e) 
        	{
        		java.awt.EventQueue.invokeLater(new Runnable() 
        		{ 
        			public void run() 
        			{
        				JFileChooser chooser = new JFileChooser();
        				chooser.setMultiSelectionEnabled(false);
        				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        				int returnVal = chooser.showOpenDialog(null);

        				if (returnVal == JFileChooser.APPROVE_OPTION) {
        					Settings.setSharedDir(chooser.getSelectedFile().getPath());
		            		PeerController.getInstance().localHostUpdateFiles();
        				}
        			}
        		});
        	}	
    	});
        		
        mnOptions.add(mntmEditSharedFolder);
        
        JMenuItem mntmEditDownloadFolder = new JMenuItem("Edit download folder");
        mntmEditDownloadFolder.addActionListener(new ActionListener() 
        {
          	public void actionPerformed(ActionEvent e) 
          	{
        		java.awt.EventQueue.invokeLater(new Runnable() 
        		{ 
        			public void run() 
        			{
        				JFileChooser chooser = new JFileChooser();
        				chooser.setMultiSelectionEnabled(false);
        				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        				int returnVal = chooser.showOpenDialog(null);

        				if (returnVal == JFileChooser.APPROVE_OPTION) {
        					Settings.setDownloadDir(chooser.getSelectedFile().getPath());
        				}
        			}
        		});
        	}
    	});
        
        mnOptions.add(mntmEditDownloadFolder);
      
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		BestandenPanel bestandenPanel = new BestandenPanel();
		tabbedPane.addTab("Peers", null, bestandenPanel, null);
		
		DownloadManager downloadManager = new DownloadManager();
		tabbedPane.addTab("Downloads", null, downloadManager, null);
	}
	
	private void actionExit() 
	{
		PeerController.getInstance().stopServers();
		System.exit(0);
    }
}	